package com.da.javatest.leetCode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author chenlida
 * @date 2019/1/10 14:22
 * @description
 */
public class Code318 {
    public static void main(String[] args) {
        System.out.println(new Code318()
            .maxProduct(new String[] {"eae", "ea", "aaf", "bda", "fcf", "dc", "ac", "ce", "cefde", "dabae"}));
    }

    public int maxProduct(String[] words) {
        return getMax(words);
    }

    private int getMax(String[] words) {
        Set<Character> s1 = new HashSet<>();
        Set<Character> s2 = new HashSet<>();
        int max = 0;
        for (int i = 0; i < words.length; i++) {
            for (char c : words[i].toCharArray()) {
                s1.add(c);
            }
            int m1 = s1.size();
            for (int j = i + 1; j < words.length; j++) {
                for (char c : words[j].toCharArray()) {
                    s2.add(c);
                }
                int m2 = s2.size();
                int m;
                if (m1 > m2) {
                    s1.retainAll(s2);
                    m = s1.size();
                    s1.clear();
                    for (char c : words[i].toCharArray()) {
                        s1.add(c);
                    }
                    m1 = s1.size();
                } else {
                    s2.retainAll(s1);
                    m = s2.size();
                }
                if (m == 0) {
                    //长度减去交集的长度为最大长度
                    max = Math.max(max, words[i].length() * words[j].length());
                }
                s2.clear();
            }
            s1.clear();
        }
        return max;
    }
}
